package com.example.shushiyunsst.fragment.main.transaction.child.kline_chart

import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.MyApp
import com.example.shushiyunsst.R
import com.example.shushiyunsst.util.flutter_bridgt.EventChannelManager
import com.example.shushiyunsst.util.flutter_bridgt.FLUTTER_TO_NATIVE
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.RenderMode
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodChannel
import org.json.JSONObject

class KlineLandscapeFragment : BaseFragmentEx(){

    companion object{
        fun newInstance(): KlineLandscapeFragment {
            val args = Bundle()
            val fragment = KlineLandscapeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var flutterFragment: FlutterFragment

    private var mMethodChannel: MethodChannel? = null

    private val eventChannel =
        EventChannelManager.instance?.getEventChannel(MyApp.K_Line_Chart_Land_Panel)

    override fun getLayoutId(): Any {
       return R.layout.kline_land_scape_fragment
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        flutterFragment = FlutterFragment.withCachedEngine(MyApp.K_Line_Chart_Land_Panel)
            .renderMode(RenderMode.texture)
            .build<FlutterFragment>()

        childFragmentManager.beginTransaction()
            .add(R.id.WalletView, flutterFragment)
            .commitAllowingStateLoss()

        val mFlutterEngine = FlutterEngineCache.getInstance().get(MyApp.K_Line_Chart_Land_Panel)

        mMethodChannel = MethodChannel(mFlutterEngine?.dartExecutor, FLUTTER_TO_NATIVE)

        mMethodChannel?.setMethodCallHandler(methodCallHandler)

        if (QMUIDisplayHelper.isPortrait()){
            QMUIDisplayHelper.setLandscape(_mActivity)
        }
    }

    private val  methodCallHandler: MethodChannel.MethodCallHandler=
        MethodChannel.MethodCallHandler {
                call, result ->
            when(call.method){
                "screen_option_p"->{
                    val shareUrl = call.argument<String>("shareUrl")?:""
                    Log.v("shareUrl=", shareUrl)
                    result.success("处理完成")
                    onBackPressedSupport()
                }
                "requireNativeData"->{
                    val param = call.argument<String>("param")?:""
//                    Toast.makeText(_mActivity,param,Toast.LENGTH_SHORT).show()
                    Log.v("requireNativeData=", param)
                    result.success("处理完成")
                    rootView.postDelayed({
                        requireKLineData(param)
                    },1000)

                }
                else->{
                    result.notImplemented()
                }
            }

        }

    private fun requireKLineData(param:String) {
        val obj =  JSONObject(param)
        val ktype = obj["title"].toString()
        val map = mapOf(
            "ktype" to ktype
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryKLineData)
            .success {
                Log.d("requireKLineData", "requireKLineData_KlineLandscape: $it")
                eventChannel?.send(it)
            }
            .failure {
                eventChannel?.send("[]")
            }
            .error { _, _ ->
                eventChannel?.send("[]")
            }
            .build().post()
    }

    override fun onBackPressedSupport(): Boolean {
        if (QMUIDisplayHelper.isLandscape()){
            QMUIDisplayHelper.setPortrait(_mActivity)
        }
        return super.onBackPressedSupport()
    }


}